package cn.tedu.mall.seckill;

import cn.tedu.mall.common.config.MallCommonConfiguration;
import cn.tedu.mall.seckill.init.SeckillInitializer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@Import(MallCommonConfiguration.class)
@MapperScan("cn.tedu.mall.seckill.mapper")
@EnableDubbo
public class SeckillApplication {
    public static void main(String[] args) {
        /*
        run 方法调用,spring容器,springboot启动流程开始加载了,我们要在run之前,添加一个
        异步的监听回调.监听器作用就是从程序开始一直到启动结束,
        观察,监控程序的状态(条件事件event),满足监听要求的时候,才开始运行监听方法
         */
        //先配置,提供好启动的SpringApplication
        SpringApplication springApplication
                = new SpringApplication(SeckillApplication.class);
        //如果我想监听不同事件,提供多个监听器 addLisener
        springApplication.addListeners(new ApplicationListener<ApplicationReadyEvent>() {
            @Override
            public void onApplicationEvent(ApplicationReadyEvent applicationEvent) {
                //在调用回调方法内部预热我的数据,
                //因为spring应用已经启动,我可以在这里使用所有容器bean对象
                //容器 上下文中,已经存在了我们需要的所有bean对象
                ConfigurableApplicationContext applicationContext
                        = applicationEvent.getApplicationContext();
                SeckillInitializer initializer = applicationContext.getBean(SeckillInitializer.class);
                initializer.initStart();
                initializer.syncStock();
            }
        });
        springApplication.run(args);
    }
}
